<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.data()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/data/">数据操作</a></span><span class="category-divider"> | </span><span class="category"><a href="/category/utilities/">工具类</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.data/" target="_blank">jQuery.data()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
			    存储任意数据到指定的元素并且/或者返回设置的值。<div class="toc">
<h4><span>Contents:</span></h4>
<ul class="toc-list">
<li>
<a href="#jQuery-data1">jQuery.data( element, key, value )</a><ul><li>jQuery.data( element, key, value )</li></ul>
</li>
<li>
<a href="#jQuery-data2">jQuery.data( element, key )</a><ul>
<li>jQuery.data( element, key )</li>
<li>jQuery.data( element )</li>
</ul>
</li>
</ul>
</div><article class="entry method" id="jQuery-data1"><h2 class="section-title">
<span class="name">jQuery.data( element, key, value )</span><span class="returns">返回: <a href="/Types/#Object">Object</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>存储任意数据到指定的元素,返回设置的值。</p>
<ul class="signatures"><li class="signature" id="jQuery-data-element-key-value">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.2.3/">1.2.3</a></span>jQuery.data( element, key, value )</h4>
<ul>
<li>
<div><strong>element</strong></div>
<div>类型: <a href="/Types/#Element">Element</a>
</div>
<div>要存储数据的DOM对象</div>
</li>
<li>
<div><strong>key</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>存储的数据名</div>
</li>
<li>
<div><strong>value</strong></div>
<div>类型: <a href="/Types/#Object">Object</a>
</div>
<div>新数据值</div>
</li>
</ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><strong>注意：</strong>这是一个底层的方法，你应该用<code><a href="/data">.data()</a></code>代替。 </p>
<p><code>jQuery.data()</code> 方法允许我们在DOM元素上附加任意类型的数据,避免了循环引用的内存泄漏风险。如果 DOM 元素是通过 jQuery 方法删除的或者当用户离开页面时，jQuery 同时也会移除添加在上面的数据。我们可以在一个元素上设置不同的值，并获取这些值：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>jQuery.data(document.body, <span class="string">'foo'</span>, <span class="number">52</span>);</code></div></div><div class="container"><div class="line"><code>jQuery.data(document.body, <span class="string">'bar'</span>, <span class="string">'test'</span>);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p><em>注意:</em> 这个方法目前并不提供在XML文档上跨平台设置，作为Internet Explorer不允许在XML文档中通过自定义属性附加数据。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4><span class="desc">在 div 元素上先存储再获取数据。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">blue;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  The values stored were</code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  and</code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">span</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"><span class="keyword">var</span> div = $(<span class="string">"div"</span>)[<span class="number">0</span>];</span></code></div></div><div class="container"><div class="line"><code>    jQuery.data(div, <span class="string">"test"</span>, { first: <span class="number">16</span>, last: <span class="string">"pizza!"</span> });</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"span:first"</span>).text(jQuery.data(div, <span class="string">"test"</span>).first);</code></div></div><div class="container"><div class="line"><code>    $(<span class="string">"span:last"</span>).text(jQuery.data(div, <span class="string">"test"</span>).last);<span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article><article class="entry method" id="jQuery-data2"><h2 class="section-title">
<span class="name">jQuery.data( element, key )</span><span class="returns">返回: <a href="/Types/#Object">Object</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>
			返回用<code>jQuery.data(element, name, value)</code>储存在元素上的相应名字的数据，或者元素上完整的数据存储</p>
<ul class="signatures">
<li class="signature" id="jQuery-data-element-key">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.2.3/">1.2.3</a></span>jQuery.data( element, key )</h4>
<ul>
<li>
<div><strong>element</strong></div>
<div>类型: <a href="/Types/#Element">Element</a>
</div>
<div>T要关联数据的DOM对象</div>
</li>
<li>
<div><strong>key</strong></div>
<div>类型: <a href="/Types/#String">String</a>
</div>
<div>存储的数据名</div>
</li>
</ul>
</li>
<li class="signature" id="jQuery-data-element">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.4/">1.4</a></span>jQuery.data( element )</h4>
<ul><li>
<div><strong>element</strong></div>
<div>类型: <a href="/Types/#Element">Element</a>
</div>
<div>要关联数据的DOM对象</div>
</li></ul>
</li>
</ul>
<div class="longdesc" id="entry-longdesc-1">
<p><strong>注意</strong>这是一个底层的方法，你应该用<code><a href="/data">.data()</a></code>代替。 </p>
<p><strong> 关于 HTML5 data-* 属性:</strong> 这个低层次的方法不检索的<code>data-*</code>属性，
			除非<code><a href="/data">.data()</a></code>方法已经返回了它们。</p>
<p><code>jQuery.data()</code>方法允许我们在DOM元素上附加任意类型的数据,避免了循环引用的内存泄漏风险。我们可以在一个元素上设置不同的值，并获取这些值：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>alert(jQuery.data( document.body, <span class="string">'foo'</span> ));</code></div></div><div class="container"><div class="line"><code>alert(jQuery.data( document.body ));</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>上面几行代码alert  <code>body</code>元素上设置的值。若果先前没有设置任何值，那么将返回一个空字符串。</p>
<p>调用<code>jQuery.data(element)</code>时将获取一个JavaScript对象，它包含了元素上所有存储的数据。jQuery内部自身使用这个方法来绑定数据，如事件处理器，所以不要以为这对象只包含你的代码中储存的数据。</p>
<p><em>注意:</em> 这个方法目前并不提供在XML文档上跨平台设置，作为Internet Explorer不允许XML 文档中通过自定义属性附加数据。</p>
</div>
<section class="entry-examples" id="entry-examples-1"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-1-0">
<h4><span class="desc">获取存放在元素上名叫 "blah" 的数据。</span></h4>
<div class="syntaxhighlighter xml ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
<div class="line n4">4</div>
<div class="line n5">5</div>
<div class="line n6">6</div>
<div class="line n7">7</div>
<div class="line n8">8</div>
<div class="line n9">9</div>
<div class="line n10">10</div>
<div class="line n11">11</div>
<div class="line n12">12</div>
<div class="line n13">13</div>
<div class="line n14">14</div>
<div class="line n15">15</div>
<div class="line n16">16</div>
<div class="line n17">17</div>
<div class="line n18">18</div>
<div class="line n19">19</div>
<div class="line n20">20</div>
<div class="line n21">21</div>
<div class="line n22">22</div>
<div class="line n23">23</div>
<div class="line n24">24</div>
<div class="line n25">25</div>
<div class="line n26">26</div>
<div class="line n27">27</div>
<div class="line n28">28</div>
<div class="line n29">29</div>
<div class="line n30">30</div>
<div class="line n31">31</div>
<div class="line n32">32</div>
<div class="line n33">33</div>
<div class="line n34">34</div>
<div class="line n35">35</div>
<div class="line n36">36</div>
<div class="line n37">37</div>
<div class="line n38">38</div>
<div class="line n39">39</div>
<div class="line n40">40</div>
<div class="line n41">41</div>
<div class="line n42">42</div>
<div class="line n43">43</div>
<div class="line n44">44</div>
<div class="line n45">45</div>
<div class="line n46">46</div>
<div class="line n47">47</div>
<div class="line n48">48</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="doctype">&lt;!DOCTYPE html&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">html</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">style</span>&gt;</span><span class="css"></span></code></div></div><div class="container"><div class="line"><code><span class="tag">div</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">5</span>px;</span></span> <span class="rule"><span class="attribute">background</span>:<span class="value">yellow;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">button</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">5</span>px;</span></span> <span class="rule"><span class="attribute">font-size</span>:<span class="value"><span class="number">14</span>px;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">p</span> <span class="rules">{ <span class="rule"><span class="attribute">margin</span>:<span class="value"><span class="number">5</span>px;</span></span> <span class="rule"><span class="attribute">color</span>:<span class="value">blue;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code><span class="tag">span</span> <span class="rules">{ <span class="rule"><span class="attribute">color</span>:<span class="value">red;</span></span> <span class="rule">}</span></span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;/<span class="title">style</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">script</span> <span class="attribute">src</span>=<span class="value">"https://code.jquery.com/jquery-latest.js"</span>&gt;</span><span class="javascript"></span><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">head</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code>  <span class="tag">&lt;<span class="title">div</span>&gt;</span>A div<span class="tag">&lt;/<span class="title">div</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span>&gt;</span>Get "blah" from the div<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span>&gt;</span>Set "blah" to "hello"<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span>&gt;</span>Set "blah" to 86<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">button</span>&gt;</span>Remove "blah" from the div<span class="tag">&lt;/<span class="title">button</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">p</span>&gt;</span>The "blah" value of this div is <span class="tag">&lt;<span class="title">span</span>&gt;</span>?<span class="tag">&lt;/<span class="title">span</span>&gt;</span><span class="tag">&lt;/<span class="title">p</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;<span class="title">script</span>&gt;</span><span class="javascript"></span></code></div></div><div class="container"><div class="line"><code>$(<span class="string">"button"</span>).click(<span class="keyword">function</span>(e) {</code></div></div><div class="container"><div class="line"><code>  <span class="keyword">var</span> value, div = $(<span class="string">"div"</span>)[<span class="number">0</span>];</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  <span class="keyword">switch</span> ($(<span class="string">"button"</span>).index(<span class="keyword">this</span>)) {</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">case</span> <span class="number">0</span> :</code></div></div><div class="container"><div class="line"><code>      value = jQuery.data(div, <span class="string">"blah"</span>);</code></div></div><div class="container"><div class="line"><code>      <span class="keyword">break</span>;</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">case</span> <span class="number">1</span> :</code></div></div><div class="container"><div class="line"><code>      jQuery.data(div, <span class="string">"blah"</span>, <span class="string">"hello"</span>);</code></div></div><div class="container"><div class="line"><code>      value = <span class="string">"Stored!"</span>;</code></div></div><div class="container"><div class="line"><code>      <span class="keyword">break</span>;</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">case</span> <span class="number">2</span> :</code></div></div><div class="container"><div class="line"><code>      jQuery.data(div, <span class="string">"blah"</span>, <span class="number">86</span>);</code></div></div><div class="container"><div class="line"><code>      value = <span class="string">"Stored!"</span>;</code></div></div><div class="container"><div class="line"><code>      <span class="keyword">break</span>;</code></div></div><div class="container"><div class="line"><code>    <span class="keyword">case</span> <span class="number">3</span> :</code></div></div><div class="container"><div class="line"><code>      jQuery.removeData(div, <span class="string">"blah"</span>);</code></div></div><div class="container"><div class="line"><code>      value = <span class="string">"Removed!"</span>;</code></div></div><div class="container"><div class="line"><code>      <span class="keyword">break</span>;</code></div></div><div class="container"><div class="line"><code>  }</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code>  $(<span class="string">"span"</span>).text(<span class="string">""</span> + value);</code></div></div><div class="container"><div class="line"><code>});</code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">script</span>&gt;</span></code></div></div><div class="container"><div class="line"><code> </code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">body</span>&gt;</span></code></div></div><div class="container"><div class="line"><code><span class="tag">&lt;/<span class="title">html</span>&gt;</span></code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></section>
</div></article> </div>
</article>